package com.itheima.cglib;

import com.itheima.service.UserService;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class UserServiceCglibProxy {
    public static UserService createUserServiceCglibProxy(Class clazz){

        //创建enhance对象
        Enhancer enhancer = new Enhancer();
        //指定enhancer的父类型是UserServiceImpl
        enhancer.setSuperclass(clazz);
        //设置回调方法
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                Object obj = methodProxy.invokeSuper(o, args);
                System.out.println("刮大白");
                System.out.println("刷粉墙");
                return obj;
            }
        });

        return (UserService)enhancer.create();
    }
}
